package com.easy.service.impl;

import com.easy.bean.ConsumptionRecord;
import com.easy.mapper.ConsumptionRecordDao;
import com.easy.service.ConsumptionRecordService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class ConsumptionRecordServiceimpl implements ConsumptionRecordService {

    @Autowired
    ConsumptionRecordDao consumptionRecordDao;

    @Override
    public ConsumptionRecord add(ConsumptionRecord consumptionRecord) {
        //新增数据
        consumptionRecordDao.add(consumptionRecord);
        //通过主键查询数据库中的数据
        ConsumptionRecord result = consumptionRecordDao.one(consumptionRecord.getId());
        return result;
    }

    @Override
    public int del(Integer id) {
        return consumptionRecordDao.del(id);
    }

    @Override
    public ConsumptionRecord edit(ConsumptionRecord consumptionRecord) {
        consumptionRecordDao.edit(consumptionRecord);
        ConsumptionRecord result = consumptionRecordDao.one(consumptionRecord.getId());
        return result;
    }

    @Override
    public List<ConsumptionRecord> all() {
        return consumptionRecordDao.all();
    }

    @Override
    public ConsumptionRecord one(Integer id) {
        return consumptionRecordDao.one(id);
    }

    @Override
    public void page(Page page, ConsumptionRecord consumptionRecord) {
        int count = consumptionRecordDao.pageCount(consumptionRecord);
        Object data = consumptionRecordDao.page(page, consumptionRecord);
        page.setCount(count);
        page.setData(data);
    }

    @Override
    public BigDecimal todayIncome() {
        BigDecimal count = consumptionRecordDao.todayIncome();
        return count == null ? new BigDecimal(0) : count;
    }

    @Override
    public BigDecimal getIncomeByDate(String date) {
        return consumptionRecordDao.todayIncomeA(date);
    }

    @Override
    public List<Map<String, Object>> todayIncomeByMethod() {
        return consumptionRecordDao.todayIncomeByMethod();
    }

}
